package com.lanou3g.code0423;

import java.net.*;

/**
 * Created by dllo on 18/4/23.
 */
public class SendClient {
    public static void main(String[] args) {
        try {
            //暂时不需要接收数据,所以随机绑定一个端口号即可
            DatagramSocket ds = new DatagramSocket();
            String s = "Hello World";
            byte[] buf = s.getBytes();//数据数组
            DatagramPacket dp = new DatagramPacket(buf,
                    buf.length, new InetSocketAddress("127.0.0.1", 7788));
            // 255.255.255.255 广播发送
            //要发送到  的 目的地 地址,端口号
            ds.send(dp);//发送
            byte[]bytes=new byte[1024];
            DatagramPacket packet=new DatagramPacket(bytes,bytes.length);
            ds.receive(packet);//接收数据
            String data =new String(packet.getData(),0,packet.getLength());
            System.out.println(data);//显示数据
            ds.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
